/*************************************************************************
	> File Name: 202.快乐数.c
	> Author: Maureen 
	> Mail: Maureen@qq.com 
	> Created Time: 四  9/ 2 17:58:56 2021
 ************************************************************************/

//判断数字n是否为快乐数，本质是链表判环问题

int getNext(int x) {
    int z = 0;
    while (x) {
        z += (x % 10) * (x % 10);
        x /= 10;
    }
    return z;
}

bool isHappy(int n) {
    //利用快慢指针判环
    int slow = n, fast = n;
    do {
        slow = getNext(slow);
        fast = getNext(getNext(fast));
    } while (slow != fast && fast != 1);
    return fast == 1;
}
